package pl.un4giv3n.mightvsmagic.action.clan;

import java.util.List;

import javax.persistence.EntityManager;

import pl.un4giv3n.mightvsmagic.entity.ClanBattle;
import pl.un4giv3n.mightvsmagic.entity.ClanWar;

public class ShowClanWarDetailsAction extends ClanAction{

	private static final long serialVersionUID = 1L;
	private Long warId;
	private ClanWar war;
	private List<ClanBattle> battles;
	private boolean otherClan;

	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		user = getSessionUser(entityManager);
		if (user == null) {
			addActionError("Zaloguj się");
			entityManager.close();
			return LOGIN;
		}
		if(!otherClan){
			clan = user.getClan();
		}
		if(!validateData(entityManager)){
			entityManager.close();
			return ERROR;
		}
		battles = war.getBattles();
		return SUCCESS;
	}

	
	private boolean validateData(EntityManager entityManager) {
		boolean valid = true;
		if(warId == null){
			addActionError("Nieprawidłowe ID wojny");
			valid = false;
		}else{
			war = entityManager.find(ClanWar.class, warId);
			if(war == null){
				addActionError("Nieprawidłowe ID wojny");
				valid = false;
			}
		}
		return valid;
	}

	public Long getWarId() {
		return warId;
	}

	public void setWarId(Long warId) {
		this.warId = warId;
	}


	public ClanWar getWar() {
		return war;
	}


	public void setWar(ClanWar war) {
		this.war = war;
	}


	public List<ClanBattle> getBattles() {
		return battles;
	}


	public void setBattles(List<ClanBattle> battles) {
		this.battles = battles;
	}


	public boolean isOtherClan() {
		return otherClan;
	}


	public void setOtherClan(boolean otherClan) {
		this.otherClan = otherClan;
	}

}
